package com.ymt.bpm.engine.listener;

import com.ymt.bpm.engine.listener.engine.exception.EngineException;
import com.ymt.bpm.engine.listener.engine.exception.ErrorCode;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Johnny on 2016/12/23.
 */
@Component
public class CommonEventDispatcher {

    /**
     * Logger
     */
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * handler registry
     */
    //private static Map<ActivitiEventType, Class<EventHandler>> registry;

    public void dispatch(ActivitiEvent activitiEvent) throws EngineException {
        logger.debug("dispatching event: "+activitiEvent.getType());
        try {

        } catch (Exception e) {
            throw new EngineException(
                    ErrorCode.ATVITIY_FAIL,
                    "Activity failed on event:"+activitiEvent.getType(),
                    e);
        }
    }

    /*public void dispatch(ActivitiEvent activitiEvent) throws EngineException {
        logger.debug("dispatching event: "+activitiEvent.getType());
        Class<EventHandler> clazz = registry.get(activitiEvent.getType());
        if (clazz==null) {
            throw new ConfigurationException(ErrorCode.CFG_NO_EVENTHANDLER,
                    "EventHandler for activity event type is missing:"+activitiEvent.getType(), null);
        } else {
            EventHandler handler = SpringContext.getBean(clazz);
            logger.debug("Get handler for "+activitiEvent.getType()+", class="+clazz.getName());
            handler.handle(activitiEvent);
        }
    }*/

    /*public static void registerHandlers() {
        if (registry==null) {
            registry = new HashMap<ActivitiEventType, Class<EventHandler>>();
        }

        registry.put(ActivitiEventType.PROCESS_STARTED , EventHandler.class);
        registry.put(ActivitiEventType.PROCESS_COMPLETED , EventHandler.class);
//        registry.put(ActivitiEventType.PROCESS_COMPLETED_WITH_ERROR_END_EVENT , EventHandler.class);
        registry.put(ActivitiEventType.PROCESS_CANCELLED , EventHandler.class);

        registry.put(ActivitiEventType.ACTIVITY_STARTED , EventHandler.class);
        registry.put(ActivitiEventType.ACTIVITY_COMPLETED , EventHandler.class);

//        registry.put(ActivitiEventType.ACTIVITY_CANCELLED , EventHandler.class);
//        registry.put(ActivitiEventType.ACTIVITY_SIGNALED , EventHandler.class);
//        registry.put(ActivitiEventType.ACTIVITY_COMPENSATE , EventHandler.class);
//        registry.put(ActivitiEventType.ACTIVITY_MESSAGE_RECEIVED , EventHandler.class);
//        registry.put(ActivitiEventType.ACTIVITY_ERROR_RECEIVED , EventHandler.class);

        registry.put(ActivitiEventType.SEQUENCEFLOW_TAKEN , EventHandler.class);

//        registry.put(ActivitiEventType.TASK_CREATED , EventHandler.class);
//        registry.put(ActivitiEventType.TASK_ASSIGNED , EventHandler.class);
//        registry.put(ActivitiEventType.TASK_COMPLETED , EventHandler.class);

//        registry.put(ActivitiEventType.VARIABLE_CREATED , EventHandler.class);
//        registry.put(ActivitiEventType.VARIABLE_UPDATED , EventHandler.class);
//        registry.put(ActivitiEventType.VARIABLE_DELETED , EventHandler.class);

//        registry.put(ActivitiEventType.HISTORIC_PROCESS_INSTANCE_CREATED , EventHandler.class);
//        registry.put(ActivitiEventType.HISTORIC_PROCESS_INSTANCE_ENDED , EventHandler.class);
//        registry.put(ActivitiEventType.HISTORIC_ACTIVITY_INSTANCE_CREATED , EventHandler.class);
//        registry.put(ActivitiEventType.HISTORIC_ACTIVITY_INSTANCE_ENDED , EventHandler.class);

//        registry.put(ActivitiEventType.UNCAUGHT_BPMN_ERROR , EventHandler.class);

//        registry.put(ActivitiEventType.MEMBERSHIP_CREATED , EventHandler.class);
//        registry.put(ActivitiEventType.MEMBERSHIP_DELETED , EventHandler.class);
//        registry.put(ActivitiEventType.MEMBERSHIPS_DELETED , EventHandler.class);

//        registry.put(ActivitiEventType.ENTITY_CREATED , EventHandler.class);
//        registry.put(ActivitiEventType.ENTITY_INITIALIZED , EventHandler.class);
//        registry.put(ActivitiEventType.ENTITY_UPDATED , EventHandler.class);
//        registry.put(ActivitiEventType.ENTITY_DELETED , EventHandler.class);
//        registry.put(ActivitiEventType.ENTITY_SUSPENDED , EventHandler.class);
//        registry.put(ActivitiEventType.ENTITY_ACTIVATED , EventHandler.class);
//
//        registry.put(ActivitiEventType.ENGINE_CREATED , EventHandler.class);
//        registry.put(ActivitiEventType.ENGINE_CLOSED , EventHandler.class);
//
//        registry.put(ActivitiEventType.TIMER_FIRED , EventHandler.class);
//        registry.put(ActivitiEventType.JOB_CANCELED , EventHandler.class);
//        registry.put(ActivitiEventType.JOB_EXECUTION_SUCCESS , EventHandler.class);
//        registry.put(ActivitiEventType.JOB_EXECUTION_FAILURE , EventHandler.class);
//        registry.put(ActivitiEventType.JOB_RETRIES_DECREMENTED , EventHandler.class);
//          registry.put(ActivitiEventType.CUSTOM , CustomEventHandler.class);
    }*/

}
